// 2010-04-20 - M. Caputo

#include <QtOpenGL>

#include "ColoredMode.h"

QString ColoredMode::name()
{
	return tr("Colored Mode");
}

void ColoredMode::setSettings()
{
	glEnable(GL_DEPTH_TEST);	
}

void ColoredMode::unsetSettings()
{
	glDisable(GL_DEPTH_TEST);
}

void ColoredMode::draw(IScene *scene, const QColor & defaultColor)
{
	int cp = scene->polygonsCount();
	for(int i=0; i<cp; i++) {
		CPolygon poly = scene->polygon(i);
		
		if (poly.hasColor()) {
			glColor3f(
				poly.color().redF(),
				poly.color().greenF(),
				poly.color().blueF()
			);
		} else {
			glColor3f(
				defaultColor.redF(),
				defaultColor.greenF(),
				defaultColor.blueF()
			);
		}
		
		int cv = poly.verticesCount();
		glBegin( GL_POLYGON );
			for(int j=0; j<cv; j++) {
				CVertex vert = poly.vertex(j);
				
				if (vert.hasColor()) {
					glColor3f(
						vert.color().redF(),
						vert.color().greenF(),
						vert.color().blueF()
					);
				} if (poly.hasColor()) {
					glColor3f(
						poly.color().redF(),
						poly.color().greenF(),
						poly.color().blueF()
					);
				} else {
					glColor3f(
						defaultColor.redF(),
						defaultColor.greenF(),
						defaultColor.blueF()
					);
				}
				
				float *data = vert.vertex();
				glVertex3f(
					data[0],
					data[1],
					data[2]
				); 
			}
		glEnd();
	}
}
